package com.zjmzxfzhl.modules.flowable.listener;

import com.alibaba.fastjson.JSON;
import com.zjmzxfzhl.common.core.util.SecurityUtils;
import org.flowable.engine.runtime.ProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

public abstract class BusinessCallListener extends BaseListener {
    private static final long serialVersionUID = -5140234938739863473L;
    protected Logger logger = LoggerFactory.getLogger(getClass());

//    @Autowired
//    private RestTemplate restTemplate;
    @Value("${listener.url}")
    private String url;

    public void callBack(String pocessInstanceId, String restUrl, String params) {
        RestTemplate restTemplate=new RestTemplate();
        String paramsJson = null;
        restUrl = url + restUrl;
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(pocessInstanceId).singleResult();
            paramMap.put("businessKey", processInstance.getBusinessKey());
            paramMap.put("user", SecurityUtils.getUserId());//执行用户
            this.setParams(params, paramMap);
            paramsJson = JSON.toJSONString(paramMap);
            //执行dubbo方法
            logger.info("开始调用业务系统接口" + restUrl + ",业务参数:" + paramsJson);
            restTemplate.postForObject(restUrl, paramsJson, String.class);
        } catch (Exception e) {
            logger.error("调用业务系统的方法失败", e);
//            FlowBuesinessException fbe = new FlowBuesinessException(clazzName,
//                    method, version, paramsJson, e.getMessage());
//            this.createWfBuesinessException(fbe);
        }
    }

    //添加容错信息
//    private void createWfBuesinessException(FlowBuesinessException fbe) {
//        try {
//            flowBuesinessExceptionService.insertFlowBuesinessException(fbe);
//        } catch (Exception e) {
//            logger.error("创建调用异常出错", e);
//        }
//    }


}
